package com.actions;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import com.bean.CostcheckProduct;
import com.opensymphony.xwork2.ActionSupport;
import com.serviceImpl.CostcheckProductServiceImpl;
import com.serviceImpl.UtilSupport;

public class CostcheckProductAction extends ActionSupport {

	private UtilSupport util;	//公用类
	private CostcheckProductServiceImpl costcheckProductBiz; //核价service
	
	//集合
	private List<CostcheckProduct> allCostcheckProduct;	//接收所有的需核价
	
	//查询字段
	private int offset;			//当前页
	private int pageSize=10;		//每页显示几条
	private int totalcount;		// 总记录数
	private int totalpage; 		// 总页数
	
	private String productCode; //产品编码
	private Double priceSt;	//开始价格
	private Double priceEd;	//结束价格
	private String timeSt;	//开始时间
	private String timeEd;	//结束时间 
	private	int chId;	//核价id
	
	
	private String refreshList;//封装刷新的页面
	private String titleName;//封装title名字
	private String msg;//封装jsp提示语
	
	
	
	//实体类
	private CostcheckProduct costcheckProduct;	//核价员类
	

	
	//set、get
	public CostcheckProductAction() {
		allCostcheckProduct=new ArrayList();	//初始化集合
		
		
	}
	
	public UtilSupport getUtil() {
		return util;
	}

	public void setUtil(UtilSupport util) {
		this.util = util;
	}

	public CostcheckProductServiceImpl getCostcheckProductBiz() {
		return costcheckProductBiz;
	}

	public void setCostcheckProductBiz(
			CostcheckProductServiceImpl costcheckProductBiz) {
		this.costcheckProductBiz = costcheckProductBiz;
	}

	public List<CostcheckProduct> getAllCostcheckProduct() {
		return allCostcheckProduct;
	}

	public void setAllCostcheckProduct(List<CostcheckProduct> allCostcheckProduct) {
		this.allCostcheckProduct = allCostcheckProduct;
	}

	public CostcheckProduct getCostcheckProduct() {
		return costcheckProduct;
	}

	public void setCostcheckProduct(CostcheckProduct costcheckProduct) {
		this.costcheckProduct = costcheckProduct;
	}
	
	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public Double getPriceSt() {
		return priceSt;
	}

	public void setPriceSt(Double priceSt) {
		this.priceSt = priceSt;
	}

	public Double getPriceEd() {
		return priceEd;
	}

	public void setPriceEd(Double priceEd) {
		this.priceEd = priceEd;
	}

	public String getTimeSt() {
		return timeSt;
	}

	public void setTimeSt(String timeSt) {
		this.timeSt = timeSt;
	}

	public String getTimeEd() {
		return timeEd;
	}

	public void setTimeEd(String timeEd) {
		this.timeEd = timeEd;
	}

	public int getChId() {
		return chId;
	}

	public void setChId(int chId) {
		this.chId = chId;
	}

	public String getRefreshList() {
		return refreshList;
	}

	public void setRefreshList(String refreshList) {
		this.refreshList = refreshList;
	}

	public String getTitleName() {
		return titleName;
	}

	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 产品核价的显示
	 * @return
	 */
	public String getCostcheckProductList(){
		HttpServletRequest request = ServletActionContext.getRequest();
 		StringBuffer sqlbf = new StringBuffer("select * from audit.costcheck_product WHERE 0=0 ");
 		
 		String pcodeSql = "";
 		String priceSql ="";
 		String timeSql = "";
 		
 		try {
 			//根据编码
 			this.productCode = request.getParameter("productCode");
			if(productCode!=null&&!productCode.isEmpty()){
				productCode=new String(productCode.trim().getBytes("ISO-8859-1"),"UTF-8");
				
				pcodeSql = "and product_code like '%"+productCode+"%'";
				
				sqlbf.append(pcodeSql);
			}
			
			//根据价格
			String pSt = request.getParameter("priceSt");//起始价格
			String pEd = request.getParameter("priceEd");//结束价格
			
			if(pSt!=null&&!pSt.isEmpty()&&pEd!=null&&!pEd.isEmpty()){
				this.priceSt=Double.parseDouble(pSt);
				this.priceEd=Double.parseDouble(pEd);
				
				if (priceSt<priceEd) {
					priceSql="and( price  between "+priceSt+" and "+priceEd+")";
				}else {
					priceSql="and( price  between "+priceEd+" and "+priceSt+")";
				}
				
				sqlbf.append(priceSql);
			}
			
			//根据时间
			this.timeSt = request.getParameter("timeSt");//开始时间
			this.timeEd = request.getParameter("timeEd");//结束时间
			
			if(timeSt!=null&&!timeSt.isEmpty()&&timeEd!=null&&!timeEd.isEmpty()){
				//格式化时间
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
				
				Date sDate = sim.parse(timeSt);
				Timestamp s = new Timestamp(sDate.getTime());
				
				Date eDate = sim.parse(timeEd);
				Timestamp e = new Timestamp(eDate.getTime());
				
				
				timeSql="and (create_time  between '"+s+"' and '"+e+"')";
				
				sqlbf.append(timeSql);
			}

			String sql =sqlbf.toString();
			
			totalcount = util.getTotalCount(sql);

			totalpage = totalcount % pageSize == 0 ? totalcount / pageSize
					: totalcount / pageSize + 1;

			offset = getPageOffset();
			//表名
			String table="audit.costcheck_product";
			//主键
			String pk="ch_id";
			//排序字段
			String sort="create_time";
			//当前页数
			int PageNumber=offset+1;
			
			int PageSize=pageSize;
			
			//查询条件
			String options=(pcodeSql+priceSql+timeSql)==null ? "":"0=0"+pcodeSql+priceSql+timeSql;
			allCostcheckProduct = costcheckProductBiz.getAllCostcheckProduct(table, pk, sort, PageNumber, PageSize, options);
		
 		} catch (Exception e) {
			// TODO: handle exception
		}
 		
 		return "show";
	}
	
	
	// Added by JSL : 获取翻页偏移量(实际上是将要翻到的页面的页索引，页索引从 0 开始)
	private int getPageOffset() {
		HttpServletRequest request=ServletActionContext.getRequest();
		String ofst = request.getParameter("offset");
		int idx = 0;
		if(ofst!=null){
			idx = Integer.valueOf(ofst);
			idx = idx < 0 ? 0 : idx;                        // 超过第一页时，不再翻页
			idx = idx >= totalpage ? (totalpage-1) : idx;	// 超过最后一页时，不再翻页		
		}
		return idx;
	}
	
	/**
	 * 通过id查找核价
	 */
	public String findCostcheck(){
		HttpServletRequest request=ServletActionContext.getRequest();
		
		//接收工艺id
		String  id= request.getParameter("chId");
		chId=Integer.parseInt(id);
	
		//调用查找方法
		costcheckProduct=costcheckProductBiz.getByidCostcheckProduct(chId);
		
		return "find";
	}
	
	/**
	 * 保存
	 */
	public String addCostcheckProduct(){
		HttpServletRequest request = ServletActionContext.getRequest();
			
		Date date = new Date();// 创建一个时间对象，获取到当前的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置时间显示格式
		String str = sdf.format(date);// 将当前时间格式化为需要的类型
	
		costcheckProduct.setCreateTime(Timestamp.valueOf(str));
		costcheckProduct.setSysUser(CostcheckProductAction.getCurrentUserName());
		
		costcheckProductBiz.saveCostcheckProduct(costcheckProduct);
		
		HttpSession session = request.getSession(false);
		
		String productCode = costcheckProduct.getProductCode();
		
		refreshList = "cpgetCostcheckProductList";
		titleName = "成衣核价";
		msg = "工艺 【 "+productCode+" 】 ";
		
		return "add";
	}	
	
	/**
	 * 修改
	 */
	public String amendCostcheckProduct(){
		
		costcheckProductBiz.mergeCostcheckProduct(costcheckProduct);
		
		return "amend";
	}
	
	/**
	 * 跳到部件核价的jsp
	 */
	public String jumpCostJsp(){
		HttpServletRequest request=ServletActionContext.getRequest();	
		
		//接收工艺id
		String  id= request.getParameter("chId");
		chId=Integer.parseInt(id);
				
		return "jump";
	}
	
	
	/**
	 * 获取当前用户名
	 */
	public static String getCurrentUserName(){
		
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		//Usin loginuser=(Usin)session.getAttribute("pu");
		//String name=loginuser.getUsna();
		String name="admin";
		return name;	
	}
}
